package com.uaroadshow.finale.web.controller.course;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.finale.processor.WechatProcessor;
import com.uaroadshow.finale.web.interceptor.UserContext;
import com.uaroadshow.finale.web.interceptor.UserContextType;
import com.uaroadshow.finale.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/user-courses")
public class UserCourseController {
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView list(@Secure User user,
                             HttpServletRequest httpServletRequest) {
        
        ModelAndView modelAndView = new ModelAndView("course/user-course-list");
        
        String curl = WebUtils.getRealRequestURL(httpServletRequest);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView detail(@Secure User user,
                               @PathVariable(name = "id") Long id,
                               HttpServletRequest httpServletRequest) {
        
        ModelAndView modelAndView = new ModelAndView("course/user-course-detail");
        
        modelAndView.addObject("courseId", id);
        
        String curl = WebUtils.getRealRequestURL(httpServletRequest);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        modelAndView.addObject("user", user);
        
        return modelAndView;
        
    }
    
}
